<template>
  <view class="container">
    <image class="cover" :src="detail.image" mode="widthFix" />
    <view class="formula-detail">
      <view class="formula-title">
        <span>{{ detail.name }}</span>
        <button open-type="share" @click.stop="handleShare">
          <image class="w-24px" src="./static/share.png" mode="widthFix" />
        </button>
      </view>
      <!-- 材料准备 -->
      <view class="section">
        <view class="section-title">材料准备</view>
        <view class="ingredients" v-html="detail.steps"></view>
      </view>
      
      <!-- 制作步骤 -->
      <view class="section">
        <view class="section-title">制作步骤</view>
        <view class="steps" v-html="detail.content"></view>
      </view>
    </view>

    <view class="fixed-button" @click="goToProduct">
      文中商品
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { onShow, onHide } from "@dcloudio/uni-app";
import { getFormulaDetail } from "@/services/formula";
import { onLoad, onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app";

onLoad((options) => {
  getDetail(options.id || '')
})

onShareAppMessage(() => ({
  title: detail.value.name,
  imageUrl: detail.value.image
}))

onShareTimeline(() => ({
  title: detail.value.name,
  imageUrl: detail.value.image
}))

let enterTime = "";
let outTime = "";
let stayTime = "";
const emdlz = getApp().emdlz2

onShow(() => {
  enterTime = new Date();
})

onHide(() => {
  // 用户退出时间
  outTime = new Date();
  //停留时间(毫秒)
  stayTime = (outTime.getTime() - enterTime.getTime()) / 1000;
  emdlz('track','event','Oreo_crumbs_main_page','view','crumbs_recipe_page', stayTime);
});

const detail = ref({})

const getDetail = async (id) => {
  const { data } = await getFormulaDetail(id)
  detail.value = data
}

const handleShare = () => {
  emdlz('track','event','Oreo_crumbs_main_page','click',`crumbs_share_${detail.value.id}`);
}

const goToProduct = () => {
  uni.navigateToMiniProgram({
    appId: "wx91d27dbf599dff74",
    path: "pages/gold/item/pages/detail/index?sku=100106742822",
  });
  // uni.navigateTo({
  //   url: `/pages/goods/goods?id=${detail.value.productId}`
  // })
}
</script>

<style>
.fixed-button {
  position: fixed;
  bottom: 30px;
  right: 20px;
  background-color: #05207F;
  color: hsla(0, 0%, 100%, 0.95);
  font-size: 15px;
  width: 115px;
  height: 32px;
  border-radius: 32px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 500;
}

.container {
  min-height: 100vh;
  color: #fff;
  background-color: white;
  box-sizing: border-box;
  padding-bottom: 20px;
  .cover {
    width: 100%;
  }
}

.formula-detail {
  width: 100%;
  padding: 22px 18px;
  box-sizing: border-box;
  .formula-title {
    color: #333333;
    font-size: 18px;
    align-items: center;
    display: flex;
    justify-content: space-between;
    font-weight: 600;
    margin-bottom: 14px;
    line-height: 1;
    button {
      padding: 0;
      height: 24px;
      line-height: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: transparent;
      margin: 0;
      &::after {
        border: none;
      }
    } 
  }
}

.section {
  margin-bottom: 20px;
}

.section-title {
  width: 72px;
  height: 19px;
  background: #333333;
  border-radius: 9px;
  text-align: center;
  line-height: 19px;
  color: #fff;
  font-size: 12px;
  margin-bottom: 18px;
}

.ingredients .ingredient-item {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0;
  border-bottom: 1px solid #333;
}

.item-name {
  color: #fff;
}

.item-amount {
  color: #fff;
}

.steps .step-item {
  margin-bottom: 20rpx;
  padding: 20rpx;
  background-color: #333;
  border-radius: 10rpx;
}

.step-number {
  color: #fff;
  margin-bottom: 10rpx;
}

.step-content {
  color: #fff;
}
</style>
